package com.lv.service.adapter

import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.material.imageview.ShapeableImageView
import com.luck.picture.lib.entity.LocalMedia
import com.lv.common.imp.OnPositionTListener
import com.lv.service.R

/**
 * 作者： ygx
 * 创建日期：2022/6/24 8:10 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 占位的adapter
 */
class SeletedPic60Adapter(
    private val datas: MutableList<LocalMedia?>?,
    private var isShowDelete: Boolean = true
) :
    BaseQuickAdapter<LocalMedia?, BaseViewHolder>
        (R.layout.item_pic_60, datas) {

    override fun convert(holder: BaseViewHolder, item: LocalMedia?) {
        val index = holder.layoutPosition
        val ivImage = holder.getView<ShapeableImageView>(R.id.iv_image)
        val ivDelete = holder.getView<ImageView>(R.id.iv_delete)
        var compressPath = item?.compressPath
        if (compressPath.isNullOrEmpty()) {
            compressPath = item?.realPath
        }
        Glide.with(context).load(compressPath).into(ivImage)
        ivDelete.visibility = if (isShowDelete) View.VISIBLE else View.GONE
        ivDelete.setOnClickListener {
            // 这里有时会返回-1造成数据下标越界,具体可参考getAdapterPosition()源码，
            // 通过源码分析应该是bindViewHolder()暂未绘制完成导致，知道原因的也可联系我~感谢
            if (index != RecyclerView.NO_POSITION) {
                datas?.removeAt(index)
                notifyItemRemoved(index)
                notifyItemRangeChanged(index, datas?.size ?: 0)

                mDeletePositionListener?.onClick(index, item)
            }
        }
        ivImage.setOnClickListener {
            mPositionListener?.onClick(index, item)
        }
    }

    fun setShowDelete(isvis: Boolean) {
        isShowDelete = isvis
        notifyDataSetChanged()
    }

    var mPositionListener: OnPositionTListener<LocalMedia>? = null
    var mDeletePositionListener: OnPositionTListener<LocalMedia>? = null
}